<template>
	<u-table-column :width="widthData" :fixed="fixed" :label="label" v-bind="$props" v-on="$listeners"
		:show-overflow-tooltip="showOverflowTooltip">
		<template v-slot="scope">
			<empty v-if="$refs.preff != null" style="display: inline;padding-right: 10px;" ref="preff">
				<slot name="preff" v-bind="scope"></slot>
			</empty>
			<opmerge ref="opmerge" :mergenumber="mergenumber" @initChildren="initChildren">
				<slot v-bind="scope"></slot>
			</opmerge>
		</template>
	</u-table-column>
</template>

<script>
import common from "@/common/common.js";

export default {
	name: "uTableColumn2",
	props: {
		width: {
			type: String,
			default: ""
		},
		mergenumber: {
			type: Number,
			default: 1
		},
		fixed: {
			type: String,
			default: "right"
		},
		showOverflowTooltip: {
			type: Boolean,
			default: false
		},
		label: {
			type: String,
			default() {
				var lang = this.$store.state.language;
				var r = lang.js['operation'];
				return r;
			}
		}
	},
	data() {
		return {
			widthData: "250",
			doFirst: false,
		}
	},
	computed: {
		isMobile() {
			var r = common.tool.handleIsMobile();
			return r;
		}
	},
	created() {
		window.elTableColumn2 = this;
	},
	methods: {
		initChildren(listButton) {
			if (this.doFirst) return;
			this.doFirst = true;
			if (!!this.width) {
				this.widthData = this.width;
			} else {
				if (this.isMobile == false) {
					this.setLen(listButton);
				} else {
					if (listButton.length <= this.mergenumber) {
						this.setLen(listButton);
					} else {
						this.widthData = 60 + this.getPreffWidth();
					}
				}
				//console.log(children);
			}
		},
		getPreffWidth() {
			if (this.$refs.preff == null) return 0;
			var len2 = 0;
			var preffButton = this.$refs.preff.$children;
			if (preffButton == null || preffButton.length == 0) return 0;
			for (var i = 0; i < preffButton.length; i++) {
				var item = preffButton[i];
				len2 += item.$el.clientWidth;
			}
			len2 += (preffButton.length - 1) * 15;
			len2 += 11;
			return len2;
		},
		setLen(listButton) {
			var len = 0;
			for (var i = 0; i < listButton.length; i++) {
				var item = listButton[i];
				var width = item.obj != null && item.obj.$el != null ? item.obj.$el.clientWidth : null;
				if (!!width) {
					len += width + 5;
				} else {
					len += item.innerText.length * 15;
				}
			}
			len += (listButton.length - 1) * 11;
			len += 10 * 2;
			if (len < 60) len = 60;
			len += this.getPreffWidth();
			this.widthData = len;
		},
		created() { }
	}
}
</script>
